<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');

if ( !ISADMIN && !ican('link') ) msg('对不起，你没有权限执行本操作！');

$f = isset($_REQUEST['f'])?$_REQUEST['f']:'index';

$message = array(
	1 => '成功更新链接分组设置。',
);

if ($f == 'index') {
	
	$result = array();
	$sql = "SELECT * FROM ".DB_PREFIX."linkgroup ORDER BY listorder";
	$query = $_G['db']->query($sql);
	while ($row = $_G['db']->fetch_array($query)) {
		$row['count'] = $_G['db']->result("SELECT COUNT(*) AS num FROM ".DB_PREFIX."link WHERE gid = '$row[gid]'");
		$result[] = $row;
	}
	include_once(ADMIN_ROOT.'./template/linkgroup.tpl.php');

} elseif ($f == 'update') {
	
	$linkgroup = array();
	$linkgroup['groupname'] = check_str(getGP('newgroupname','P'));
	$linkgroup['status'] = getGP('newstatus','P','int');
	$linkgroup['listorder'] = getGP('neworderid','P','int');
	
	if (!empty($linkgroup['groupname'])) {
		$_G['db']->insert('linkgroup', $linkgroup);
	}
	$groupname = getGP('groupname','P','array');
	$status = getGP('status','P','array');
	$orderid = getGP('orderid','P','array');
	foreach ($groupname as $key => $val) {
		$key = (int)$key;
		$_G['db']->query("UPDATE ".DB_PREFIX."linkgroup SET groupname = '$val', `status` = '$status[$key]', listorder = '$orderid[$key]' WHERE gid = '$key'");
	}
	recache('link,linkgroup');
	goto_page('admin.php?m=linkgroup&message=1');

} elseif ($f == 'delete') {

	$id = getGP('id','G','int');
	$_G['db']->query("DELETE FROM ".DB_PREFIX."linkgroup WHERE gid = '$id'");
	$_G['db']->query("UPDATE ".DB_PREFIX."link SET gid = 0 WHERE gid = '$id'");
	recache('link,linkgroup');
	exit('success');
}

function get_status_options($sid = 1) {
	$html = '';
	$html .= '<option value="1" '.($sid == 1 ? 'selected="selected"':'').' >首页显示</option>';
	$html .= '<option value="2" '.($sid == 2 ? 'selected="selected"':'').' >内页显示</option>';
	$html .= '<option value="3" '.($sid == 3 ? 'selected="selected"':'').' >不显示</option>';
	return $html;
}
?>